<!--
 * @Descripttion: 
 * @version: 
 * @Author: wangmu
 * @Date: 2022-05-25 11:07:49
 * @LastEditors: wangmu
 * @LastEditTime: 2022-06-01 11:18:17
-->
<template>
	<el-dialog title="商品类目" :append-to-body="true" :visible.sync="visible" width="30%">
		<el-table ref="listPowerSupplyTab" default-expand-all :data="productGroupingTableData" tooltip-effect="dark" style="width: 100%" :row-key="getRowKeys" border
		 :append-to-body="true" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" @selection-change="handleSelectionChange">
			<el-table-column  type="selection" width="55" :reserve-selection="true" :selectable="selectable">
			</el-table-column>
			<el-table-column prop="name" label="分组名称" align="center">
			</el-table-column>
		</el-table>


		<span slot="footer" class="dialog-footer">
			<el-button @click="cancel()">取 消</el-button>
			<el-button type="primary" @click="submit()">确 定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	export default {
	  data () {
	    return {
	      visible: false,
	      productGroupingTableData: [], // 商品分组
	      productMultipleSelection: [],
	      group_slim_list: []

	}
	},
	  methods: {
	    init (val, row) {
	      this.visible = true
	      console.log(row)
	      this.productGroupingTableData = val
	      if (row.group_slim_list instanceof Array && row.group_slim_list.length > 0) {
	        row.group_slim_list.forEach(element => {
	          this.group_slim_list.push(element.id)
	          val.forEach(k => {
	            k.children.forEach(j => {
	              if (j.id == element.id) {
	                this.$nextTick(() => {
	                  this.$refs.listPowerSupplyTab.toggleRowSelection(j)
	                })
	}
	})
	})
	})
	        this.productMultipleSelection = this.group_slim_list
	};
	},
	    getRowKeys (row) {
	      this.group_slim_list.forEach(id => {
	        if (row.children) {
	          row.children.forEach(k => {
	            if (id == k.id) {
	              return k.id
	}
	})
	}
	})
	      return row.id
	    },
	    selectable (row, index) {
	      if (row.parent_id > 0) {
	        return true
	} else {
	        return false
	}
	},
	    handleSelectionChange (val) {
	      this.productMultipleSelection = []
	      for (let item of val) {
	        this.productMultipleSelection.push(item.id * 1)
	}
				// this.productMultipleSelection.join(',');
	},
	
	    cancel () {
	      this.$emit('result', false)
	    },
	
	    submit () {
	      this.$emit('result', this.productMultipleSelection)
	    }
	
	}
	}
</script>

<style>
</style>
